document.write("<script src='js/httpUtil.js'></script>");
document.write("<script src='js/common.js'></script>");

$(document).ready(function () {
    $('.counter').countUp();
    $(".banner-name").html(ls.getItem("username"))
    $(".banner-head").attr("src", imgUrl+ls.getItem("head"));
    $(".banner-department").html(JSON.parse(ls.getItem("department"))[0])
    var departments="";
    for(var i=0;i<JSON.parse(ls.getItem("department")).length;i++){
        departments = departments + " [" + JSON.parse(ls.getItem("department"))[i] +"] "
    }
    $(".banner-department").attr("data-original-title",departments);
    var sideMenu =  $("#sideMenuItem");
    for(var i=0;i<JSON.parse(ls.getItem("menus")).length;i++){
          sideMenu.append("<li class='"+JSON.parse(ls.getItem("menus"))[i].resourceName+"'>" +
                   "<a class=\"side-menu__item\" href='"+JSON.parse(ls.getItem("menus"))[i].resourceString+"'>" +
              "     <i class=\"side-menu__icon fa "+getIcon(JSON.parse(ls.getItem("menus"))[i].resourceName)+"\"></i><span class=\"side-menu__label\">"+JSON.parse(ls.getItem("menus"))[i].resourceDesc+"</span></a>" +
                   "</li>")
    }
})
$("#searchBtn").click(function (){
    var keyword = $("#search").val()
    for(var i=0;i<JSON.parse(ls.getItem("menus")).length;i++){
        if(JSON.parse(ls.getItem("menus"))[i].resourceDesc.indexOf(keyword)>=0){
            window.location.href = JSON.parse(ls.getItem("menus"))[i].resourceString;
            return false;
        }
    }
});

function getIcon(resourceName){
    switch (resourceName){
        case "s_account":
            return "fa-group"
        case "s_depart":
            return "fa-sitemap"
        case "s_role":
            return "fa-user"
        case "s_menu":
            return "fa-folder-open"
        case "s_ipWhiteList":
            return "fa-shield"
        default:
            return "fa-bars"
    }
}

$("#changePasswordPanelBtn").click(function () {
    $("#changePasswordModal").modal('show')
})

$("#changePasswordBtn").click(function () {
    var oldPassword = $("#oldPassword").val();
    var newPassword = $("#newPassword").val();
    var newPassword2 = $("#newPassword2").val();
    yorickAssert(oldPassword != "" && newPassword != ""
        && newPassword2, "请输入完整参数", "参数错误")
    yorickAssert(newPassword == newPassword2, "两次密码不一致", "新密码不一致")
    var jsonObj =
        {
            "password":newPassword2,
            "oldPassword":oldPassword
        }
    var result=httpPost(baseUrl+"/account/changePassword",jsonObj);
    if(result.flag==1) {
        toastr.success("修改密码成功", "成功");
        $('#changePasswordModal').modal('hide');
    }else{
        toastr.error("系统错误", result.msg);
    }
})

$("#logoutBtn").click(function () {
    var result=httpPost(baseUrl+"/login/doLogout","");
    if(result.flag==1){
        ls.clear()
        location.href="login.html"
    }else{
        toastr.error("请联系管理员",result.msg);
    }
})

